import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class MainWindow
{
	static JFrame jf;//主窗口
	static JButton start,settings;
	static JButton ST, JD, BU, newPlayer, reset, backToMenu;
	static JPanel panelOther, panelUserOut, panelInfoBar;
	static JPanel panelMenu, panelMaxRound;
	static JLabel labelVoid, labelNPC, labelInfoBar, labelMaxRound;
	
	static JTextArea textA;
	
	//窗口初始化
	static
	{
		jf = new JFrame("kkss01石头剪刀布");
		jf.setLocation(1000, 500);  // 设置窗体坐上顶点坐标
		jf.setSize(350, 500);   // 窗体大小
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setLocationRelativeTo(null);
	}
	
	public static void windowMenu()
	{
		
		//建立对象
		panelMenu = new JPanel();
		panelMaxRound = new JPanel();
		panelMenu.setBackground(Color.yellow);
		panelMaxRound.setBackground(Color.yellow);
		
		labelMaxRound = new JLabel("当前决胜局数："+ Rules.maxRound);
		start = new JButton("开始游戏");
		settings = new JButton("设置");
		
		panelMenu.add(start);
		panelMenu.add(settings);
		panelMaxRound.add(labelMaxRound,BorderLayout.CENTER);
		
		
		jf.add(panelMenu,BorderLayout.NORTH);
		jf.add(panelMaxRound);
		//jf.setLocation(500, 500);//窗口位置
		jf.setVisible(true);//可见
		panelMenu.repaint();
		
		start.addActionListener(new AbstractAction()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				jf.remove(panelMenu);
				windowGameStart();
			}
		});
		settings.addActionListener(new AbstractAction()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				
				jf.remove(panelMenu);
				panelMenu.repaint();
				Settings.setting();
			}
		});
	}
	public static void windowGameStart()
	{
	
		Rules.round = 0;//重置局数
		//建立对象
		panelOther = new JPanel();
		panelUserOut = new JPanel();
		panelInfoBar = new JPanel();
		panelOther.setBackground(Color.yellow);
		panelUserOut.setBackground(Color.green);
		panelInfoBar.setBackground(Color.pink);
		
		
		reset = new JButton("重置对局");
		newPlayer = new JButton("设置电脑玩家");
		backToMenu = new JButton("返回菜单");
		ST = new JButton("石头");
		JD = new JButton("剪刀");
		BU = new JButton("布");
		
		//labelVoid = new JLabel("          ");
		labelNPC = new JLabel(NPC.getName()+"：一决胜负！");
		labelInfoBar = new JLabel("选择剪刀, 石头, 布开始游戏");
		
		textA = new JTextArea("===" + Rules.maxRound+" 局决胜!===\n",15, 20);
		textA.setLineWrap(true);
		textA.setEditable(false);
		
		panelOther.add(newPlayer);
		panelOther.add(reset);
		panelOther.add(backToMenu);
		panelUserOut.add(ST);
		panelUserOut.add(JD);
		panelUserOut.add(BU);
	
		//panelUserOut.add(labelVoid);
		panelUserOut.add(labelNPC);
		panelInfoBar.add(labelInfoBar);
		
		panelUserOut.add(textA);
		
		jf.add(panelOther, BorderLayout.NORTH);
		jf.add(panelUserOut, BorderLayout.CENTER);
		jf.add(panelInfoBar, BorderLayout.SOUTH);
		
		panelUserOut.repaint();
		
		
		
		//new Listener();
		//jf.setLocation(500, 500);
		jf.setVisible(true);
		
		NPC npc1 = new NPC();
		UserPlayer user1 = new UserPlayer();
		
		// 给按钮添加监听器
		ST.addActionListener(new AbstractAction()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				labelInfoBar.setText("选择石头");
				//result = Rules.getRules(0,1);
				int result = 3;
				Rules.getRules(0, (npc1.out()));
			}
		});
		JD.addActionListener(new AbstractAction()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				labelInfoBar.setText("选择剪刀");
				Rules.getRules(1, (npc1.out()));
			}
		});
		BU.addActionListener(new AbstractAction()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				labelInfoBar.setText("选择布");
				Rules.getRules(2, (npc1.out()));
			}
		});
		reset.addActionListener(new AbstractAction()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				Rules.round = 0;
				NPC.setScore(0);
				UserPlayer.setScore(0);
				labelInfoBar.setText("选择剪刀, 石头, 布开始游戏");
				labelNPC.setText(NPC.getName()+"：一决胜负！");
				textA.setText("");
				panelUserOut.add(ST);
				panelUserOut.add(JD);
				panelUserOut.add(BU);
				panelUserOut.add(labelNPC);
				panelUserOut.add(textA);
				panelUserOut.repaint();
				textA.append(Rules.maxRound+"局决胜!\n");
			}
		});
		newPlayer.addActionListener(new AbstractAction()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				labelInfoBar.setText("* 暂时不可用");
				NPC.addNPC();
				NPC npc1 = new NPC();
			}
		});
		backToMenu.addActionListener(new AbstractAction()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				//清除已有窗口
				jf.remove(panelOther);
				jf.remove(panelUserOut);
				jf.remove(panelInfoBar);
				windowMenu();
			}
		});
	}
	
	public static void windowGameEnd()
	{
		System.out.println("调用游戏结束窗口");
		//jf.remove(panelUserOut);
		panelUserOut.remove(ST);
		panelUserOut.remove(JD);
		panelUserOut.remove(BU);
		panelUserOut.remove(labelNPC);
		panelUserOut.repaint();
		textA.append("===游戏结束！===\n");
		int x1 = UserPlayer.getScore();
		int x2 = NPC.getScore();
		if (x1>x2)
		{
			textA.append("你赢了！得分 "+ x1 +"       "+ NPC.getName() + " 得分" + x2 + "\n");
		}
		else if (x1<x2)
		{
			textA.append(NPC.getName() +" 赢了！得分 "+ x2 +"        你得分"+ x1 + "\n");
		}
		else
		{
			textA.append("* 平局！*");
		}
		if(ScoreSave.save())
		{
			System.out.println("已保存");
		}
		//jf.setVisible(true);
	}
}




	

	
	

